/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.shape.annotation.impl;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;

import who.tmap.toolkit.shape.annotation.ImageShape;

/**
 * Annotations Image
 * @author stl
 *
 */
public class ImageShapeImpl extends BaseAnnotationGraphicShapeImpl 
							implements ImageShape {

    private static Logger logger = Logger.getLogger(ImageShapeImpl.class);     
    
    private ImageHelper helper =new ImageHelper();

	public ImageShapeImpl() {
		setType( IMAGE );
	}

    /**
     * @return Returns the file.
     */
    public File getFile() {
        return helper.getFile();
    }
    
    public void setFile( File file ) {
    	helper.setFile( file );
    }
    
    /**
     * @param file The file to load.
     */
    public void loadFile(File file) throws IOException {
    	helper.loadFile( file );
    	
    	width =helper.getWidth();
    	height =helper.getHeight();
    }
    
	public void resetToOriginalSize() {
	    helper.resetToOriginalSize();
	}    
    
    public Image getImage() {
		return helper.getImage();
	}

	public void setImage(Image image) {
		helper.setImage( image );
		// STL 2009.01.13
		// Should this 2 lines embedded into ImageHelper.setImage() ?
		helper.setWidth( image.getWidth( null ) );
		helper.setHeight( image.getHeight( null ) );
	}

	public byte[] getImageBytes() {
		return helper.getImageBytes();
	}

	public void setImageBytes(byte[] imageBytes) {
		helper.setImageBytes( imageBytes );
	}
    
	public void draw(Graphics g) {
	    Graphics2D g2d =(Graphics2D)g;

	    double factor =getScreenToMapFactor() * getZoomFactor() / 100d;
	    
	    AffineTransform at =new AffineTransform();
		
	    at.translate( pt.getX(), pt.getY() );
	    at.scale( factor, factor );
	    
	    g2d.drawImage( helper.getImage(), at, null );
	    
	    bounds =new Rectangle2D.Double( pt.getX(), pt.getY()
	    							  , helper.getWidth() * factor
	    							  , helper.getHeight() * factor );
	    
	    displaySelectionHandles( g2d );
	}
	
	/**
	 * Nothing to fill for images.
	 */
	public void fill(Graphics g) {
    }		
	
	public void fill( Graphics g, Color color ) {
	}
	
	public boolean contains( Point2D p ) {
	    return bounds.contains( p );
	}
	
	public void updateShape(int handlerIdx, Point2D p) {
		/*super.updateShape( handlerIdx, p );
		
	    double factor =getScreenToMapFactor() * getZoomFactor() / 100d;
		
		bounds.setFrame( pt.getX(), pt.getY(), getWidth() *factor, getHeight() *factor);*/
	}


}
